import { getByDictCode } from '@/api/dict'
import { formatData } from '@/utils/ParamFilter'

const dictFilterMixins = {
  data() {
    return {
      dictFilters: {}
    }
  },
  methods: {
    /**
     * 初始化字典数据存到变量中
     * @param dictCode
     */
    getDictFilter(dictCode) {
      // eslint-disable-next-line no-prototype-builtins
      if (!this.dictFilters.hasOwnProperty(dictCode)) {
        getByDictCode({
          dictCode: dictCode,
          size: 1000
        }).then(res => {
          var data = formatData(res.data.records)
          this.dictFilters[dictCode] = data
        }).catch(err => {
          console.log(err)
        }).finally(() => {
        })
      }
    },
    /**
     * 字典翻译
     * @param value
     * @param dictCode
     * @returns {*}
     */
    dictTrans(value, dictCode) {
      var map = this.dictFilters[dictCode]
      if (map !== undefined) {
        var find = map.find(d => d.value === value)
        if (find !== undefined) {
          return map.find(d => d.value === value).label
        } else {
          return value
        }
      }
    },
    /**
     * 查看字典对象内容
     */
    logDict() {
      console.log(this.dictFilters)
    }
  }
}

export { dictFilterMixins }
